

package net.qiqbframework.commandhandling;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;


public class LoggingDuplicateCommandHandlerResolver implements DuplicateCommandHandlerResolver {

    private static final Logger logger = LoggerFactory.getLogger(LoggingDuplicateCommandHandlerResolver.class);
    private static final LoggingDuplicateCommandHandlerResolver INSTANCE = new LoggingDuplicateCommandHandlerResolver();


    public static LoggingDuplicateCommandHandlerResolver instance() {
        return INSTANCE;
    }

    private LoggingDuplicateCommandHandlerResolver() {
    }

    @Override
    public CommandMessageHandler resolve(@Nonnull String commandName, @Nonnull CommandMessageHandler registeredHandler, @Nonnull CommandMessageHandler candidateHandler) {

        logger.warn("A duplicate command handler was found for command [{}]. "
                        + "The handler in [{}] has been replaced by the handler in [{}].",
                commandName,
                registeredHandler.getTargetType().getName(),
                candidateHandler.getTargetType().getName()
        );
        return candidateHandler;
    }
}
